package com.deng.Service.impl;

import com.deng.Service.TagService;
import com.deng.mapper.TagMapper;
import com.deng.model.Paper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author 邓小熙
 * @Date 2022/5/17 17:49
 * @Version 1.0
 */
@Service
public class TagServiceImpl implements TagService {
    @Autowired
    TagMapper tagMapper;

    /**
     * 主页查看所有tag
     *
     * @return
     */
    @Override
    public Set<String> findAllTagName() {
        return tagMapper.findAllTagName();
    }

    /**
     * 根据tagName查找文章 并进行分页
     *
     * @param tagName
     * @param pageNum
     * @return
     */
    @Override
    public PageInfo<Paper> tagPage(String tagName, int pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<Paper> papers = tagMapper.findPaperByTagName(tagName);
        PageInfo<Paper> pageInfo = new PageInfo<>(papers);
        return pageInfo;
    }

    @Override
    public int findHasTagByPaperId(int paperId) {
        return tagMapper.findHasTagByPaperId(paperId);
    }

    /**
     * 删除文章的删除文章对应的tag
     *
     * @param paperId
     * @return
     */
    @Override
    public int delectTag(int paperId) {
        return tagMapper.delectTag(paperId);
    }
    /**
     * 根据文章的id查询tag
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findTagNamesByPaperId(int id) {
        Set<String> set = tagMapper.findTagNamesByPaperId(id);
        List<Map<String, String>> list = setToList(set);
        Map<String, Object> map = Maps.newHashMap();
        map.put("data", list);
        map.put("code", 0);
        map.put("mes", "");
        return map;
    }

    private List<Map<String,String>> setToList(Set<String> set){
        List<Map<String,String>> list = Lists.newLinkedList();
        for (String tag: set
        ) {
            Map<String,String> objectMap = new HashMap<>();
            objectMap.put("tags",tag);
            list.add(objectMap);
        }
        return list;
    }
    /**
     * 新建标签
     * @param name
     * @param id
     * @return
     */
    @Override
    public int insertTagNameByPaperId(String name, int id) {
        return tagMapper.insertTagNameByPaperId(name,id);
    }
    /**
     *  删除标签
     * @param id
     * @param name
     * @return
     */
    @Override
    public int delectTagByPaperIdAndTagName(int id, String name) {
        return tagMapper.delectTagByPaperIdAndTagName(id,name);
    }
    /**
     * 修改
     * @param newName
     * @param id
     * @param name
     * @return
     */
    @Override
    public int updateTagNameByPaperIdAndTagName(String newName, int id, String name) {
        return tagMapper.updateTagNameByPaperIdAndTagName(newName,id,name);
    }


    /**
     * 查找对应用户的所有tag
     * @param name
     * @return
     */
    @Override
    public Set<String> findTagNameByUser(String name) {
        return tagMapper.findTagNameByUser(name);
    }

    @Override
    public int insertTagByUserId(List<String> tagName, int paperId) {
        return tagMapper.insertTagByUserId(tagName,paperId);
    }
}
